Ensures(T) Method (T, String)

CuttingEdge.Conditions

Returns a new ConditionValidator that allows you to validate the postconditions of the given object.

Namespace:  CuttingEdge.Conditions
Assembly:  CuttingEdge.Conditions (in CuttingEdge.Conditions.dll)

Syntax

Visual Basic (Declaration)
Public Shared Function Ensures(Of T) ( _
	value As T, _
	argumentName As String _
) As ConditionValidator(Of T)
C#
public static ConditionValidator<T> Ensures<T>(
	T value,
	string argumentName
)
Visual C++
public:
generic<typename T>
static ConditionValidator<T>^ Ensures(
	T value, 
	String^ argumentName
)
JavaScript
JavaScript does not support generic types or methods.

Parameters

value
Type: T
The object to validate.
argumentName
Type: System..::.String
The name of the argument to validate

Type Parameters

T
The type of the object to validate.

Return Value

A new ConditionValidator containing the value and argumentName.

Examples

The following example shows a way to use the Ensures method. Shown is an IObjectBuilder interface which contract states that the BuildObject method should never return null. That contract, however, is not enforced by the compiler or the runtime. To allow this contract to be validated, the ObjectBuilderValidator class is a decorator for objects implementing the IObjectBuilder interface and it ensures that the given contract is fulfilled, by checking the return value of the called BuildObject of the wrapped IObjectBuilder.
 Copy Code
   using CuttingEdge.Conditions;
    
   public interface IObjectBuilder
   {
       /// <summary>Builds an object.</summary>
       /// <returns>Returns a newly built object. Will not return null.</returns>
       object BuildObject();
   }
    
   public class ObjectBuilderValidator : IObjectBuilder
   {
       public object BuildObject()
       {
           object obj = wrappedObjectBuilder.BuildObject();
    
           // When obj == null, a PostconditionException is thrown, with the following message:
           // "Postcondition 'the value returned by IObjectBuilder.BuildObject() should not be null'
           // failed."
           Conditions.Ensures(obj, "the value returned by IObjectBuilder.BuildObject()")
               .IsNotNull();
    
           return obj;
       }
    
       private readonly IObjectBuilder wrappedObjectBuilder;
  
       /// <summary>
       /// Initializes a new instance of the <see cref="ObjectBuilderValidator"/> class.
       /// </summary>
       /// <param name="objectBuilder">The object builder.</param>
       /// <exception cref="ArgumentNullException">
       /// Thrown when <paramref name="objectBuilder"/> is a null reference.
       /// </exception>
       public ObjectBuilderWrapper(IObjectBuilder objectBuilder)
       {
           // Throws a ArgumentNullException when objectBuilder == null.
           Condition.Requires(objectBuilder, "objectBuilder").IsNotNull();
    
           this.wrappedObjectBuilder = objectBuilder;
       }
   }
    
See the ConditionValidator<(Of <(T>)>) class for more code examples.

See Also